package it.progettoso.activity;

import it.progettoso.thread.SendMailThread;
import it.progettoso.utility.DBConnection;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CamActivity extends Activity implements SurfaceHolder.Callback {

	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private Camera mCamera;
	private boolean mPreviewRunning;
	Context context=this;
	String fileName="";
	private AudioManager manager;
	private int volume;
	String email="";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maincamera);

		mSurfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		DBConnection db = new DBConnection(context);
		if(db.open()){
			email=db.readData(DBConnection.EMAIL);
			db.close();
		}
		manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
		volume=manager.getStreamVolume(AudioManager.STREAM_SYSTEM);
		manager.setStreamVolume(AudioManager.STREAM_SYSTEM,0,-100);
	}
	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		if(mCamera==null)
			return;
		if (mPreviewRunning)
			mCamera.stopPreview();

		//setto le preferenze
		Camera.Parameters p = mCamera.getParameters();
		p.setJpegQuality(80);
		p.setRotation(90);
		p.setFlashMode(Parameters.FLASH_MODE_OFF);
		// salvo le pref
		mCamera.setParameters(p);
		mCamera.setDisplayOrientation(90);
		try {
			//lancio la preview
			mCamera.setPreviewDisplay(arg0);        
			mCamera.startPreview();
			mPreviewRunning = true;
			mCamera.takePicture(null, null, jpegCallback);
		} catch (IOException e) {
			//gestione errore
		}
	}


	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mCamera = Camera.open();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(mCamera==null)
			return;
		mCamera.stopPreview();
		mPreviewRunning = false;
		mCamera.release();


	}

	PictureCallback jpegCallback = new PictureCallback() {
		public void onPictureTaken(byte[] _data, Camera _camera) {

			try {
				File file = new File(getFilesDir(), "Foto.jpg");
				FileOutputStream outStream =new FileOutputStream(file); 
				outStream.write(_data);
				outStream.close();

				//Lettura di conferma di avvenuto salvataggio
				FileInputStream inStream = new FileInputStream(file);
				int num=inStream.read();
				inStream.close();

				fileName= getFilesDir()+ "/" + "Foto.jpg";
				String[] toArr={ email };
				if(num>0){
					Runnable sendok = new SendMailThread(fileName,num,toArr);
					Thread t = new Thread(sendok);
					t.start();		
					CamActivity.this.finish();
				}else{
					Runnable sendok = new SendMailThread(null,num,toArr);
					Thread t = new Thread(sendok);
					t.start();	
					manager.setStreamVolume(AudioManager.STREAM_SYSTEM,volume,-100);
					CamActivity.this.finish();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			CamActivity.this.finish();
		}
	};



}